#ifndef _ISTACK_H_
#define _ISTACK_H_

#include <stdlib.h>

#define DEFAULT_SIMPLE_STACK_SIZE 100

template<typename MEMTYPE>class CSimpleStack
{
public:
    CSimpleStack(unsigned int size = DEFAULT_SIMPLE_STACK_SIZE);
    ~CSimpleStack();

    int put(MEMTYPE& in);
    MEMTYPE get();
    
private:
    unsigned int m_readWriteIdx;
    unsigned int m_maxSize;
    MEMTYPE* m_pData;
};

template<typename MEMTYPE>
CSimpleStack<MEMTYPE>::CSimpleStack(unsigned int size)
{
    m_readWriteIdx = 0;
    m_maxSize = size;
    m_pData = (MEMTYPE*)malloc(sizeof(MEMTYPE)*size);
}

template<typename MEMTYPE>
CSimpleStack<MEMTYPE>::~CSimpleStack()
{
    free(m_pData);
}

template<typename MEMTYPE>
int CSimpleStack<MEMTYPE>::put(MEMTYPE& in)
{
    if (m_readWriteIdx == m_maxSize)
    {
        return -1;
    }
    m_pData[m_readWriteIdx] = in;
    m_readWriteIdx++;
}

template<typename MEMTYPE>
MEMTYPE CSimpleStack<MEMTYPE>::get()
{
    if(m_readWriteIdx == 0)
    {
        return -1;
    }
    MEMTYPE getVal = m_pData[m_readWriteIdx-1];
    m_readWriteIdx--;
    return getVal;
}

#endif

